package com.maxjay.component.simplelist.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.maxjay.component.simplelist.service.PaginationService;
import com.maxjay.system.service.BaseServiceJdbc;

public abstract class PaginationServiceJdbc extends BaseServiceJdbc implements PaginationService {

	private Logger logger = Logger.getLogger(PaginationServiceJdbcMysql.class);

	@Override
	public List<Map<String, Object>> getPaginationData(String sql, Map<String, Object> params, int startRowIndex, int endRowIndex) {
		String paginationSql = this.generatePaginationSql(sql, startRowIndex, endRowIndex);
		logger.debug("分页查询语句为："+paginationSql);
		logger.debug("参数列表为：" + params);
		return this.getSimpleJdbcTemplate().queryForList(paginationSql, params);
	}

	@Override
	public int getDataCount(String sql, Map<String, Object> params) {
		String countSql = "SELECT count(1) FROM (" + sql + ") data";
		logger.debug("查询记录总量的SQL为：" + countSql);
		logger.debug("参数列表为：" + params);
		return this.getSimpleJdbcTemplate().queryForInt(countSql, params);
	}
	
	/**
	 * 组装分页语句，根据数据库的分页语法进行封装
	 * 
	 * @param sql
	 * @param startRow
	 * @param endRow
	 * @return
	 */
	protected abstract String generatePaginationSql(String sql, int startRow, int endRow);

}
